Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue-144 Using aws-lambda-go-api-proxy to enable echo Framework and Lambda with ALB #146

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ikegam1
Copy link

@ikegam1 ikegam1 commented Sep 18, 2022

Issue #144

Description of changes:
I used aws-lambda-go-api-proxy to use echo Framework and Lambda with ALB.
However, it failed because the response type from Lambda to ALB is different.
To support this, I made an adapter for echo.

The base is the code of the api gateway.

スクリーンショット 2022-09-18 16 42 47

Details

ALB event format

https://docs.aws.amazon.com/lambda/latest/dg/services-alb.html

example (code fragment)

e := echo.New()
echoLambda := echoadapter.NewALB(e)

func handler(ctx context.Context, req events.ALBTargetGroupRequest) (events.ALBTargetGroupResponse, error) {
        return echoLambda.ProxyWithContext(ctx, req)
}

func main() {
        lambda.Start(handler)
}

An example using sam can be found here

go test result

••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
Ran 96 of 96 Specs in 0.002 seconds
SUCCESS! -- 96 Passed | 0 Failed | 0 Pending | 0 Skipped
PASS
ok      github.com/awslabs/aws-lambda-go-api-proxy/core 0.349s

•
Ran 3 of 3 Specs in 0.001 seconds
SUCCESS! -- 3 Passed | 0 Failed | 0 Pending | 0 Skipped
PASS
ok      github.com/awslabs/aws-lambda-go-api-proxy/echo 0.237s

debug result

request & response

% curl -XPOST http://xxxxxxxx.ap-northeast-1.elb.amazonaws.com/t/ -d '{"msg":"aaa"}' -H "application/json"
"hello world!"

cloud watch logs(debug)

START RequestId: 4900fcb0-e0d4-4ed7-968c-9938ffaea6b5 Version: $LATEST
--
2022/09/18 06:31:47 req: events.ALBTargetGroupRequest{HTTPMethod:"POST", Path:"/t/", QueryStringParameters:map[string]string{}, MultiValueQueryStringParameters:map[string][]string(nil), Headers:map[string]string{"accept":"*/*", "content-length":"13", "content-type":"application/x-www-form-urlencoded", "host":"xxxxxxxx.elb.amazonaws.com", "user-agent":"curl/7.64.1", "x-amzn-trace-id":"Root=1-6326bb53-576053df0f5f6f8c40fa7b23", "x-forwarded-for":"000.000.000.0000", "x-forwarded-port":"80", "x-forwarded-proto":"http"}, MultiValueHeaders:map[string][]string(nil), RequestContext:events.ALBTargetGroupRequestContext{ELB:events.ELBContext{TargetGroupArn:"arn:aws:elasticloadbalancing:ap-northeast-1:00000000:targetgroup/xxxxxxxx/xxxxxx"}}, IsBase64Encoded:true, Body:"eyJtc2ciOiJhYWEifQ=="}
2022/09/18 06:31:47 routing t
2022/09/18 06:31:47 reqBody: "{\"msg\":\"aaa\"}"
{     "time": "2022-09-18T06:31:47.559549149Z",     "id": "",     "remote_ip": "000.000.000.000",     "host": "",     "method": "POST",     "uri": "/t",     "user_agent": "curl/7.64.1",     "status": 200,     "error": "",     "latency": 26490,     "latency_human": "26.49µs",     "bytes_in": 13,     "bytes_out": 15 }
END RequestId: 4900fcb0-e0d4-4ed7-968c-9938ffaea6b5
REPORT RequestId: 4900fcb0-e0d4-4ed7-968c-9938ffaea6b5	Duration: 1.25 ms	Billed Duration: 2 ms	Memory Size: 128 MB	Max Memory Used: 33 MB

Please confirm.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@ikegam1 ikegam1 changed the title issue-144 issue-144 Using aws-lambda-go-api-proxy to enable echo Framework and Lambda with ALB Sep 18, 2022
@ikegam1 ikegam1 marked this pull request as ready for review September 18, 2022 07:53
@0angelic0
Copy link

Hello, any chance that this PR will get a look.

@0angelic0
Copy link

I tried merge this PR into my fork and it is working very well. And I even successfully added fiberadapter for ALB on top of this PR. It could be great if we can get this PR merged into this main awslabs repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants